/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package officeboost.domain;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import org.jbpm.api.*;
import org.jbpm.api.task.Task;

/**
 *
 * @author sorra
 */
@Singleton
@LocalBean
public class ProcessMgr {
    private ProcessEngine pe = null;
    private List<Lock> lockList = new ArrayList<>();
    
    @EJB
    private LogMgr logger;
    
    @PostConstruct
    public void init() {	
	Configuration configuration = new Configuration();
	pe = configuration.buildProcessEngine();
    }
    @PreDestroy
    public void shut() {
	pe.close();
    }
    
    public void deploy() {	
	pe.getRepositoryService().createDeployment().addResourceFromClasspath("pd/leave.jpdl.xml").deploy();
	logger.log("Process definitions deployed!");
    }
    
    public ProcessEngine getProcessEngine() {
	return pe;
    }
    
    public ExecutionService getExecutionService() {
	return pe.getExecutionService();
    }
    
    public TaskService getTaskService() {
	return pe.getTaskService();
    }    
    
    public String mes() {
	String mes = "<p>mes begins!</p>";
	logger.log(mes);	
	
	ProcessInstance processInstance = pe.getExecutionService().startProcessInstanceByKey("LeaveForm");
	logger.log("ProcessInstance started: "+processInstance.getId());
	
	
	Task task1 = pe.getTaskService().findPersonalTasks("teacher").get(0);
	pe.getTaskService().completeTask(task1.getId(), "accept");
	mes += "<p>task1: "+task1.getId()+"</p>\n";
	
	Task task2 = pe.getTaskService().findPersonalTasks("student_office").get(0);
	pe.getTaskService().completeTask(task2.getId(), "accept");
	mes += "<p>task2: "+task2.getId()+"</p>\n";
	
	Task task3 = pe.getTaskService().findPersonalTasks("education_office").get(0);
	pe.getTaskService().completeTask(task3.getId(), "accept");
	mes += "<p>task3: "+task3.getId()+"</p>\n";
	
	Task task4 = pe.getTaskService().findPersonalTasks("school").get(0);
	pe.getTaskService().completeTask(task4.getId(), "accept");
	mes += "<p>task4: "+task4.getId()+"</p>\n";
	
	return mes;
    }
    
}
